`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    14:21:38 04/20/2011 
// Design Name: 
// Module Name:    ForwardingUnit 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module ForwardingUnit(IDEX_Rs, IDEX_Rt, EXMEM_Rd, EXMEM_RegWrite, MEMWB_Rd, 
	MEMWB_RegWrite, ForwardA, ForwardB);

	input [3:0] IDEX_Rs;
	input [3:0] IDEX_Rt;
	input [3:0] EXMEM_Rd;
	input EXMEM_RegWrite;
	input [3:0] MEMWB_Rd;
	input MEMWB_RegWrite;
	output [1:0] ForwardA;
	output [1:0] ForwardB;
	
	reg [1:0] ForwardA;
	reg [1:0] ForwardB;
	
	always @ (*)
	begin
		ForwardA = 2'b00;
		ForwardB = 2'b00;
		
		//EX Hazard
		if (EXMEM_RegWrite && (EXMEM_Rd !=0) && (EXMEM_Rd == IDEX_Rs))
			ForwardA = 2'b10;
		if (EXMEM_RegWrite && (EXMEM_Rd !=0) && (EXMEM_Rd == IDEX_Rt))
			ForwardB = 2'b10;
		
		//MEM Hazard
		if (MEMWB_RegWrite && (MEMWB_Rd != 0) && EXMEM_RegWrite && 
				(EXMEM_Rd != 0) && (EXMEM_Rd != IDEX_Rs))
			ForwardA = 2'b01;
		if (MEMWB_RegWrite && (MEMWB_Rd != 0) && EXMEM_RegWrite && 
				(EXMEM_Rd != 0) && (EXMEM_Rd != IDEX_Rt))
			ForwardB = 2'b01;
	end

endmodule
